<?php

/**
 * BannerModel 
 */
class bannerModel
{
    /**
     * Base
     * @var object 
     */
    public $base;

    /**
     * Redis 连接
     * @var resources 
     */
    private $redis;

    public function __construct(& $base)
    {
        $this->base = & $base;
        $this->redis = & $this->base->redis;
    }
    
    public function getGroupBanner(){
        $result = $this->redis->get(REDIS_PREFIX.'GroupBanner');
        if($result)
        {
            $result = json_decode($result, true);
        }
        !$result && $result = array();
        if(!empty($result)){
            usort($result,function($a,$b){return $a['sort']>$b['sort'];});
        }
        return $result;
    }
    
    public function getBbsFocusPic(){
        $data = $this->redis->get(REDIS_PREFIX.'bbsFocusPic');
        $result = json_decode($data, true);
        $result = $result ? $result: array();

        if($this->base->userInfo && $result)
        {
            foreach($result as $key => $value)
            {
                if(stripos($value['linkUrl'], '/grn') > 10)
                {
                    $result[$key]['linkUrl'] = $value['linkUrl'] . '?token=' . $this->base->token;
                }
            }
        }

        return $result;
    }
    
    public function getGearNewsBanner(){
        $data = $this->redis->GET(REDIS_PREFIX.'gearNewsBanner');
        return $data ? json_decode($data,true) : [];
    }
    
    public function getActBanner(){
        $result = $this->redis->get(REDIS_PREFIX."ActBanner");
        if($result)
        {
            $result = json_decode($result, true);
        }
        !$result && $result = array();
        return $result;
    }
    
    public function getUserBanner(){
        $result = $this->redis->hget(REDIS_PREFIX."Banner",'userBanner');
        
        if(!$result){
            $result = [];
        }else{
            $result = json_decode($result,true);
            usort($result,function($a,$b){return $a['sort']>$b['sort'];});
        }
        return $result;
    }
    
    public function getGroupBanner2(){
        $result = $this->redis->hget(REDIS_PREFIX."Banner",'groupBanner2');
        if(!$result){
            $result = [];
        }else{
            $result = json_decode($result,true);
            usort($result,function($a,$b){return $a['sort']>$b['sort'];});
        }
        return $result;
    }
    
    public function getActRecBanner(){
        $result = $this->redis->hget(REDIS_PREFIX."Banner",'actRecBanner');
        if(!$result){
            $result = [];
        }else{
            $result = json_decode($result,true);
            usort($result,function($a,$b){return $a['sort']>$b['sort'];});
        }
        return $result;
    }
    
    public function getBanner($key){
        $result = $this->redis->hget(REDIS_PREFIX."Banner",$key);
        if(!$result){
            $result = [];
        }else{
            $result = json_decode($result,true);
            usort($result,function($a,$b){return $a['sort']>$b['sort'];});
        }
        return $result;
    }
    
    public function setBanner($posi,array $banner){
        if(!$posi){
            return;
        }
        if(in_array($posi,['GroupBanner','ActBanner'])){
            $this->redis->set(REDIS_PREFIX.$posi, json_encode($banner));
        }else{
            $this->redis->hset(REDIS_PREFIX."Banner",$posi,json_encode($banner));
        }
    }
}